package com.ytz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ytz.controller.dto.OrderInfoDTO;
import com.ytz.entity.OrderInfo;
import com.ytz.enums.OrderStatus;
import com.ytz.mapper.OrderInfoMapper;
import com.ytz.service.IOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ytz
 * @since 04-18
 */
@Slf4j
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {

    @Override
    public OrderInfo createOrder(OrderInfoDTO orderInfoDTO) {
        log.info("创建订单");
        OrderInfo orderInfo = new OrderInfo();

        orderInfo.setTitle(orderInfoDTO.getTitle());
        //订单编号
        orderInfo.setOrderNo(String.valueOf(System.currentTimeMillis()));
        //用户id
        orderInfo.setUserId(123456L);
        //金额 分
        orderInfo.setTotalFee(orderInfoDTO.getTotalFee());
        //订单状态
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
        //创建时间
        orderInfo.setCreateTime(LocalDateTime.now());

        int insert = baseMapper.insert(orderInfo);
        if(insert>0){
            log.info("订单创建成功");
        }
        return orderInfo;
    }

    @Override
    public OrderInfo findByOrderNo(String orderNo) {
        LambdaQueryWrapper<OrderInfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderInfo::getOrderNo,orderNo);
        List<OrderInfo> orderInfos = baseMapper.selectList(lambdaQueryWrapper);
        if(orderInfos!=null&&!orderInfos.isEmpty()){
            return orderInfos.get(0);
        }
        return null;
    }



    @Override
    public void saveCodeUrl(Long id, String codeUrl) {
        UpdateWrapper<OrderInfo> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("code_url",codeUrl);
        updateWrapper.eq("id",id);
        baseMapper.update(null,updateWrapper);

    }


}
